package com.qifenqian.turbot.module.model

import android.content.Context
import com.qifenqian.turbot.base.BaseModel
import com.qifenqian.turbot.module.contract.RestaurantContract
import com.qifenqian.turbot.module.entity.request.PhotoRequest
import com.qifenqian.turbot.module.entity.response.UploadEntity
import com.qifenqian.turbot.module.entity.response.restaurant.CuisineEntity
import com.qifenqian.turbot.module.entity.response.restaurant.RestaurantEntity
import com.qifenqian.turbot.module.entity.response.restaurant.RestaurantPhotoEntity
import com.qifenqian.turbot.module.entity.response.restaurant.RestaurantTableEntity
import com.qifenqian.turbot.net.ModelCallback
import com.qifenqian.turbot.net.NetObserver
import io.reactivex.Observable

/**
 * @author H
 * @createTime 2018/12/25 16:05
 * @description
 * @see
 * @since
 */
class RestaurantModel(context: Context) : BaseModel(context), RestaurantContract.Model {

    override fun deleteTableInfo(id: Int, callback: ModelCallback<Nothing>) {
        mClient!!.deleteTableInfo(id)
                .subscribe(object : NetObserver<Nothing>(mContext, false) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun uploadFile(filePaths: MutableList<String>, callback: ModelCallback<UploadEntity>) {
        val observableList = mutableListOf<Observable<UploadEntity>>()
        for (filePath in filePaths) {
            val uploadFile = mClient!!.uploadFile(filePath)
            observableList.add(uploadFile)
        }
        var observable = Observable.mergeArray<UploadEntity>(observableList[0])
        when (filePaths.size) {
            1 -> observable = Observable.mergeArray<UploadEntity>(observableList[0])
            2 -> observable = Observable.mergeArray<UploadEntity>(observableList[0], observableList[1])
            3 -> observable = Observable.mergeArray<UploadEntity>(observableList[0], observableList[1], observableList[2])
            4 -> observable = Observable.mergeArray<UploadEntity>(observableList[0], observableList[1], observableList[2], observableList[3])
            5 -> observable = Observable.mergeArray<UploadEntity>(observableList[0], observableList[1], observableList[2], observableList[3], observableList[4])
            6 -> observable = Observable.mergeArray<UploadEntity>(observableList[0], observableList[1], observableList[2], observableList[3], observableList[4], observableList[5])
            7 -> observable = Observable.mergeArray<UploadEntity>(observableList[0], observableList[1], observableList[2], observableList[3], observableList[4], observableList[5], observableList[6])
            8 -> observable = Observable.mergeArray<UploadEntity>(observableList[0], observableList[1], observableList[2], observableList[3], observableList[4], observableList[5], observableList[6], observableList[7])
        }
        observable!!.subscribe(object : NetObserver<UploadEntity>(mContext, true) {
            override fun onSuccess(msg: String?, t: UploadEntity?) {
                callback.onSuccess(msg, t)
            }

            override fun onFailure(code: Int, msg: String?) {
                callback.onFailure(code, msg)
            }
        })

    }

    override fun savePhotoList(request: PhotoRequest, callback: ModelCallback<Nothing>) {
        mClient!!.savePhotoList(request)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

//    override fun savePhotoList(ids: MutableList<Int>, restaurantPhotoPaths: MutableList<String>, callback: ModelCallback<Nothing>) {
//        mClient!!.savePhotoList(ids, restaurantPhotoPaths)
//                .subscribe(object : NetObserver<Nothing>(mContext, true) {
//                    override fun onSuccess(msg: String?, t: Nothing?) {
//                        callback.onSuccess(msg = msg)
//                    }
//
//                    override fun onFailure(code: Int, msg: String?) {
//                        callback.onFailure(code, msg)
//                    }
//                })
//    }

    override fun getPhotoList(callback: ModelCallback<List<RestaurantPhotoEntity>>) {
        mClient!!.getPhotoList()
                .subscribe(object : NetObserver<List<RestaurantPhotoEntity>>(mContext, true) {
                    override fun onSuccess(msg: String?, t: List<RestaurantPhotoEntity>?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

//    override fun saveTableInfo(callback: ModelCallback<Nothing>) {
//        mClient!!.saveTableInfo(restaurantId)
//                .subscribe(object : NetObserver<Nothing>(mContext, true) {
//                    override fun onSuccess(msg: String?, t: Nothing?) {
//                        callback.onSuccess(msg = msg)
//                    }
//
//                    override fun onFailure(code: Int, msg: String?) {
//                        callback.onFailure(code, msg)
//                    }
//                })
//    }

    override fun saveTableInfo(saveTableRequest: RestaurantTableEntity, callback: ModelCallback<Nothing>) {
        mClient!!.saveTableInfo(saveTableRequest)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun getTableInfo(callback: ModelCallback<RestaurantTableEntity>) {
        mClient!!.getTableInfo()
                .subscribe(object : NetObserver<RestaurantTableEntity>(mContext, true) {
                    override fun onSuccess(msg: String?, t: RestaurantTableEntity?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun changeRestaurantIntroduction(introduction: String, callback: ModelCallback<Nothing>) {
        mClient!!.changeRestaurantIntroduction(introduction)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun changePersonConsume(consumePerPerson: Int, callback: ModelCallback<Nothing>) {
        mClient!!.changePersonConsume(consumePerPerson)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun changeAddress(province: String, city: String, area: String, address: String, callback: ModelCallback<Nothing>) {
        mClient!!.changeAddress(province, city, area, address)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun changeBusinessTime(openTime: String, closeTime: String, callback: ModelCallback<Nothing>) {
        mClient!!.changeBusinessTime(openTime, closeTime)
                .subscribe(object : NetObserver<Nothing>(mContext, false) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun setAvailableChooseSeat(onlineReservation: Int, callback: ModelCallback<Nothing>) {
        mClient!!.setAvailableChooseSeat(onlineReservation)
                .subscribe(object : NetObserver<Nothing>(mContext, false) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun changeRestaurantPhone(restaurantPhone: String, callback: ModelCallback<Nothing>) {
        mClient!!.changeRestaurantPhone(restaurantPhone)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun chooseCuisine(cuisine: String, callback: ModelCallback<Nothing>) {
        mClient!!.chooseCuisine(cuisine)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun getCuisineList(callback: ModelCallback<List<CuisineEntity>>) {
        mClient!!.getCuisineList()
                .subscribe(object : NetObserver<List<CuisineEntity>>(mContext, false) {
                    override fun onSuccess(msg: String?, t: List<CuisineEntity>?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }

                })
    }

    override fun getRestaurantInfo(showDialog: Boolean, callback: ModelCallback<RestaurantEntity>) {
        mClient!!.getRestaurantInfo()
                .subscribe(object : NetObserver<RestaurantEntity>(mContext, showDialog) {
                    override fun onSuccess(msg: String?, t: RestaurantEntity?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }

                })
    }

    override fun editShortName(shortName: String, callback: ModelCallback<Nothing>) {
        mClient!!.editShortName(shortName)
                .subscribe(object : NetObserver<Nothing>(mContext, false) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

}
